<?php
/**
-------------------------
GNU GPL COPYRIGHT NOTICES
-------------------------
This file is part of FlexicaCMS.

FlexicaCMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlexicaCMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlexicaCMS.  If not, see <http://www.gnu.org/licenses/>.*/

/**
 * $Id$
 *
 * @author FlexicaCMS team <contact@flexicacms.com>
 * @link http://www.flexicacms.com/
 * @copyright Copyright &copy; 2009-2010 Gia Han Online Solutions Ltd.
 * @license http://www.flexicacms.com/license.html
 */
class ModuleMenus extends FWidget
{
    public function run() {
        $criteria = new CDbCriteria();
        $criteria->condition = 'has_back_end = \'y\'';
        $criteria->order = 'ordering';
        
        $backEndModules = Module::model()->findAll($criteria);
        $moduleMenus = array();
        
        foreach ($backEndModules as $beModule) {
            $module = Yii::app()->getModule($beModule->name);
            $menus = $module->getMenus();
            
            //Fix submenu label to separate title from description
            foreach ($menus as &$menu) {
                $tmp = explode('|',$menu['label']);
                if (count($tmp) < 2) $tmp[]='';
                list($name, $desc) = $tmp;
                $menu['label'] = '<span class="sub-menu-text">
                                      <strong>'.$name.'</strong>
                                      <span>'.$desc.'</span>
                                  </span>';
            }
            //If the menu doesn't have icon, use default one
            if ($beModule->icon == '')
                $beModule->icon = Yii::app()->core->AssetUrl.'/images/module_menu_icon.png';
                
            $moduleMenus[] = array(
                'label' => '<img src="'.$beModule->icon.'" width="16" height="16" border="0" />
                            <span class="menu-text">'.$beModule->friendly_name.'</span>
                            <span class="arrow"></span>',
                'url' => '#',
                'items' => $menus,
                'linkOptions'=> array(
                    'class'=>'top-menu-item'
                ),
            );
        }    

        
        $this->widget('zii.widgets.CMenu', array(
            'items' => $moduleMenus,
            'encodeLabel'=>false
        ));
    }
}
?>